package com.poss.gui;

import com.poss.gui.api.PossGuiAction;
import com.poss.gui.api.PossLoginAction;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author shan
 */
class PossLoginForm extends JFrame implements PossGuiWindow {
    
    private JLabel lCaption;
    private JLabel lUsername;
    private JLabel lPassword;
    private JLabel lErrorField;
    private JTextField tUsername;
    private JPasswordField tPassword;
    private JButton okButton;
    private JButton cancelButton;
    private PossLoginAction loginActionBase;
    private boolean actionOk;
    
    public PossLoginForm () {
        actionOk = false;
        initUi();
    }

    private void initUi() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex); // should be written in gui_log.log
        }
        
        /* Initializing Controllers */
        lCaption = new JLabel("Login Form");
        lCaption.setFont(new Font("Monospaced 12", Font.BOLD, 16));
        lCaption.setHorizontalAlignment(SwingUtilities.CENTER);
        lCaption.setPreferredSize(new Dimension(290, 30));
        lUsername = new JLabel("Username : ");
        lUsername.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        lUsername.setHorizontalAlignment(SwingUtilities.RIGHT);
        lUsername.setPreferredSize(new Dimension(85, 25));
        lPassword = new JLabel("Password : ");
        lPassword.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
        lPassword.setHorizontalAlignment(SwingUtilities.RIGHT);
        lPassword.setPreferredSize(new Dimension(85, 25));
        lErrorField = new JLabel("");
        lErrorField.setForeground(Color.RED);
        lErrorField.setFont(new Font("Monospaced 12", Font.BOLD, 12));
        lErrorField.setHorizontalAlignment(SwingUtilities.CENTER);
        lErrorField.setPreferredSize(new Dimension(290, 25));
        tUsername = new JTextField();
        tUsername.setPreferredSize(new Dimension(200, 25));
        tPassword = new JPasswordField();
        tPassword.setPreferredSize(new Dimension(200, 25));
        okButton = new JButton(" LogIn ");
        okButton.setPreferredSize(new Dimension(145, 25));        
        cancelButton = new JButton(" Cancel ");
        cancelButton.setPreferredSize(new Dimension(145, 25));
        
        okButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (actionOk) {
                    loginActionBase.okButtonClicked();
                } else {
                    System.out.println("Action registration has been failed"); // should be written in gui_log.log
                }
            }
        });
        
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (actionOk) {
                    loginActionBase.cancelButtonClicked();
                } else {
                    System.out.println("Action registration has been failed"); // should be written in gui_log.log
                }
                
            }
        });      
        
        JPanel basePanel = new JPanel();
        basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(lCaption);
        basePanel.add(topPanel);
        
        JPanel unPanel = new JPanel(new FlowLayout());
        unPanel.add(lUsername);
        unPanel.add(tUsername);
        basePanel.add(unPanel);
        JPanel pwPanel = new JPanel(new FlowLayout());
        pwPanel.add(lPassword);
        pwPanel.add(tPassword);
        basePanel.add(pwPanel);
        
        basePanel.add(lErrorField);
        
        JPanel bottomPanel = new JPanel(new FlowLayout());
        bottomPanel.add(okButton);
        bottomPanel.add(cancelButton);
        basePanel.add(bottomPanel);
        
        add(basePanel);

        setMinimumSize(new Dimension(320, 170));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 330) / 2, (screen.height - 200) / 2);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(false);
    }

    @Override
    public boolean registerAction(PossGuiAction actionObject) {
        if (actionObject instanceof PossLoginAction) {
            loginActionBase = (PossLoginAction)actionObject;
            actionOk = true;
            return true;
        }
        actionOk = false;
        return false;
    }

    @Override
    public void showGui() {
        setVisible(true);
    }

    @Override
    public void hideGui() {
        setVisible(false);
    }
    
}
